<?php

namespace app\user\controller\order;

use app\common\controller\Backend;
use app\common\controller\Frontend;
use hehe\Str;
use think\facade\Db;

/**
 *
 */
class Detail extends Frontend {
    /**
     * Goods模型对象
     * @var object
     * @phpstan-var \app\user\model\goods\Goods
     */
    protected object $model;
    protected object $goodsModel;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['name', 'brand.name'];

    public function initialize(): void {
        parent::initialize();
        $this->orderGoodsModel = new \app\user\model\order\OrderGoods;
//        $this->model = new \app\user\model\live\Temp;
    }




    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }
        $orderModel = new \app\user\model\order\Order();

        $goods_ids = [];
        $param = $this->request->param();

        $order = $orderModel->where(['out_trade_no' => $param['out_trade_no']])->find();
        $order['status_text'] = $order->status_text;


        $order['place_order_time'] = date('m-d/H:i', $order['place_order_time']);
        $order['live_time'] = empty($order['live_time']) ? '' : date('m月d日 H点', $order['live_time']);
        $user = db::name('user')->where(['id' => $order['user_id']])->find();
        $result = Db::name('order_goods')->where(['order_id' => $order['id']])->select();
        foreach($result as $val) {
            $goods_ids[] = $val['goods_id'];
        }

        $where = [
            ['order_id', '=', $order['id']]
        ];
        $list = $this->orderGoodsModel->with(['brand', 'goods'])->where($where)->select();


        foreach ($list as $key => $val) {
            $list[$key]['purchase_price'] = number_format($val['purchase_price'], 0, '.', ',');
            $list[$key]['total_price'] = number_format($val['total_price'], 0, '.', ',');
        }
        $order['total_price'] = number_format($order['total_price']);

        $this->success('', [
            'list' => $list,
            'remark' => get_route_remark(),
            'user' => $user,
            'order' => $order
        ]);
    }




}